package thread.word_matching;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/*
    任务类
 */
public class WordMatchingTask implements Callable<Distance> {
    private String word;//要计算的单词
    private List<String> words;//单词列表
    private DistanceEditing editing;//编辑距离计算类
    private int start;//开始位置
    private int end;//结束位置

    public WordMatchingTask() {
    }

    public WordMatchingTask(String word, List<String> words, int start, int end) {
        this.word = word;
        this.words = words;
        this.start = start;
        this.end = end;
    }

    @Override
    public Distance call() throws Exception {
        editing = new DistanceEditing();
        List<String> minlist = new ArrayList<>();
        int minDistance = Integer.MAX_VALUE;
        int dis;
        for (int i = start;i<end;i++) {
            dis = editing.distance(word, words.get(i));
            if (dis < minDistance) {//最小距离更改
                minDistance = dis;
                minlist.clear();//清空list
            }
            if (dis == minDistance) //最小距离相同，添加到list
                minlist.add(words.get(i));
        }
        Distance distance = new Distance();
        distance.setDistance(minDistance);
        distance.setWordList(minlist);
        System.out.println("task distance = "+distance);
        return distance;
    }
}
